﻿using System;

namespace Alfa.Units {
	public struct Rankine : ITemperatureUnit, IComparable<Rankine>, IEquatable<Rankine> {
		private decimal degrees;
		public Rankine(decimal Degrees) {
			this.degrees = Degrees;
		}

		public static explicit operator Celsius(Rankine rankine) {
			return new Celsius((rankine.degrees - 491.67m) * 5m / 9m);
		}

		public static explicit operator Fahrenheit(Rankine rankine) {
			return new Fahrenheit(rankine.degrees - 459.67m);
		}

		public static explicit operator Kelvin(Rankine rankine) {
			return new Kelvin(rankine.degrees * 5m / 9m);
		}

		public static implicit operator decimal(Rankine rankine) {
			return rankine.degrees;
		}

		decimal ITemperatureUnit.Degrees { get { return this.degrees; } }

		string ITemperatureUnit.Symbol { get { return "°R"; } }

		public int CompareTo(object obj) {
			if (obj is ITemperatureUnit) {
				return ((ITemperatureUnit)this).CompareTo((ITemperatureUnit)obj);
			} else
				throw new InvalidCastException(obj.GetType( ).Name + " does not implement ITemperatureUnit");
		}

		int IComparable<ITemperatureUnit>.CompareTo(ITemperatureUnit other) {
			return this.degrees.CompareTo(((Rankine)other).degrees);
		}

		bool IEquatable<ITemperatureUnit>.Equals(ITemperatureUnit other) {
			return this.degrees.Equals(((Rankine)other).degrees);
		}

		int IComparable<Rankine>.CompareTo(Rankine other) {
			return this.degrees.CompareTo(other.degrees);
		}

		bool IEquatable<Rankine>.Equals(Rankine other) {
			return this.degrees.Equals(other.degrees);
		}
	}
}
